﻿$(function() {
	//获取用户权限name
	var $user_role_name = $(".user_role_name").text();
	var user_id=0;
	//判断是否是管理员用户
	if($user_role_name!="root"){
		user_id=$(".user_id").text();
	}else{
		$("#tj").css("display","none");
	}
	
	// 获取查询的类型
	var $commodity_type_id = 0;
	//获取模糊查询
	var $gocommoditName =0;
	
	//进入页面默认加载的数据
	startUp=function(){
		
		$.ajax({
			"url" : "/queryCommodityId",
			"type" : "get",
			"dataType" : "json",
			"success" : function(result) {
				classification(result);
			}
		})
		
		$.ajax({
			"url" : "/commodityList1",
			"data" : "userId="+user_id,
			"type" : "get",
			"dataType" : "json",
			"success" : function(result) {
				each(result);
				pageInfo(result);
			}
		})
		
	}
	
	//分类下拉框数据绑定
	classification=function(result){
		for (var i = 0; i < result.length; i++) {
			var option="<option value='"+result[i].commoditytype_id+"'>"+result[i].commoditytype_name+"</option>";
			$(".types").append(option);
		}
		
	}
	
	//进入页面默认加载的数据
	startUp();
	

	// 获取模糊查询的关键字
//	var $searchText = $(".searchText").val();

	// 获取查询的类型
//	var $commodity_type_id = $(".type_id").text();
//	$(".types").find("option[value='" + $commodity_type_id + "']").attr("ed", "true");
	
	//显示分页
	pageInfo=function(result){
		$(".pa").html("");
		var pag = "<span>当前页：<span class='pagenum' >"+result.pageNum+"</span>"+
					"<span>----</span> "+
					"<span>总页数：<span class='pages'>"+result.pages+"</span>"+
					"<br />"+
					"<a class='sy' href='#'>首页 </a>"+
					"<a class='syy' href='#'>上一页 </a>"+
					"<a class='xyy' href='#'>下一页 </a>"+
					"<a class='my' href='#'>尾页 </a>"+
				"</span>";
		$(".pa").append(pag);
	}	

	
	

	//首页按钮
	$(".sy").live("click",function() {
		// 获取模糊查询的关键字
		var $searchText = $(".searchText").val();

		// 获取查询的类型
		var $commodity_type_id = $(".type_id").text();
		// 获取当前页
		var $pagenum = $(".pagenum").text();
		// 获取总页数
		var $pages = $(".pages").text();
		
		var url = "commodityList1?pageNum=1&commodity_type_id="+ $commodity_type_id + "&gocommoditName="+ $searchText;
		PageUrl(url);
		return false;
	})

	//上一页按钮
	$(".syy").live("click",function() {
		// 获取模糊查询的关键字
		var $searchText = $(".searchText").val();

		// 获取查询的类型
		var $commodity_type_id = $(".type_id").text();
		// 获取当前页
		var $pagenum = $(".pagenum").text();
		// 获取总页数
		var $pages = $(".pages").text();
		
		if ($pagenum > 1) {
			var url = "commodityList1?pageNum=" + ($pagenum - 1)+ "&commodity_type_id=" + $commodity_type_id+ "&gocommoditName=" + $searchText;
			PageUrl(url);
			return false;
		}
	})
	
	//下一页按钮
	$(".xyy").live("click",function() {
		// 获取模糊查询的关键字
		var $searchText = $(".searchText").val();
		
		
		// 获取当前页
		var $pagenum = $(".pagenum").text();
		// 获取总页数
		var $pages = $(".pages").text();

		if ($pagenum < $pages) {
			$pagenum = parseInt($pagenum) + 1;
			var url = "commodityList1?pageNum=" + $pagenum + "&commodity_type_id="+ $commodity_type_id + "&gocommoditName="+ $searchText;
			PageUrl(url);
			return false;
		}
	})
	
	//末页按钮
	$(".my").live("click",function() {
		// 获取模糊查询的关键字
		var $searchText = $(".searchText").val();

		// 获取查询的类型
		var $commodity_type_id = $(".type_id").text();
		// 获取当前页
		var $pagenum = $(".pagenum").text();
		// 获取总页数
		var $pages = $(".pages").text();
		
		var url = "commodityList1?pageNum=" + $pages+ "&commodity_type_id=" + $commodity_type_id+ "&gocommoditName=" + $searchText;
		PageUrl(url);
		return false;
	})
	
	

	//分页通用的url
	PageUrl = function(url) {
		
		url=url+"&userId="+user_id;
		$(".listtr").remove();
		$.ajax({
			"url" : url,
			"type" : "get",
			"dataType" : "json",
			"success" : function(result) {
				each(result);
				pageInfo(result);
			}
		})
	}
	
	//添加商品
	$(".am-form-horizontal").submit(function() {
		
		$.ajax({
			"url" : "/addCommodity",
			"data" : $("form").serialize(),
			"type" : "get",
			"dataType" : "text",
			"success" : function(result) {
				if (result == "true") {
					alert("添加成功！");
					location.href = "inventory";
				} else {
					alert("添加失败！");
				}
			},
			"error" : function(errorResult) {
				alert("出错了");
			}
		})
	})

	//显示个添加子页切换
	$(".tabs").slide({
		trigger : "click"
	});

	
	//模糊搜索
	$(".gosearch").live("click",function() {
		$gocommoditName = $(".searchText").val();
		url="/commodityList1?gocommoditName="+$gocommoditName+"&commodity_type_id="+$commodity_type_id+"&userId="+user_id;
		$.ajax({
			"url" : url,
			"type" : "get",
			"dataType" : "json",
			"success" : function(result) {
				each(result);
				pageInfo(result);
			}
		})
	})
	
	

	// 分类查询
	$(".gotype").live("click",function() {
		$commodity_type_id = $(".types").val();
		url="/commodityList1?gocommoditName="+$gocommoditName+"&commodity_type_id="+$commodity_type_id+"&userId="+user_id;
		$.ajax({
			"url" : url,
			"type" : "get",
			"dataType" : "json",
			"success" : function(result) {
				each(result);
				pageInfo(result);
			}
		})
	})
	
	
	
	
	//遍历集合
	each=function(result){
		if(result.pages==0){
			alert("没有内容");
			return ;
		}
		$("tbody").html("");
		for (var i = 0; i < result.list.length; i++) {
			var commodity_state = "不可用";
			var editcommodity_state = "恢复可用";
			if (result.list[i].commodity_state == 1) {
				commodity_state = "可用";
				editcommodity_state = "设置不可用";
			}
			if(result.list[i].commodity_state == 3){
				commodity_state = "待审核";
				editcommodity_state = "通过审核";
			}
			if(result.list[i].commodity_state == 4){
				commodity_state = "审核不通过";
				editcommodity_state = "通过审核";
			}
			var $tr="";
			if(user_id==0){
				if(commodity_state=="待审核"||commodity_state=="审核不通过"){
					$tr = "<tr class='listtr'>"+ 
					"<td class='id'>"+result.list[i].commodity_id+"</td>"+
					"<td>"+ result.list[i].commodity_name+ "</td>"+
					"<td><img height='50' width='50' src='"+result.list[i].commodity_img+"' /></td>"+
					"<td>"+ result.list[i].commodity_count+ "</td>"+
					"<td>"+ result.list[i].commodity_quantitysold+ "</td>"+
					"<td>￥"+ result.list[i].commodity_price+ "</td>"+
					"<td>￥"+ result.list[i].commodity_tradeprice+ "</td>"+
					"<td>"+ commodity_state+ "</td>"+
					"<td class='delete'>" + 
						"<button class='btn'>"+
							"<i class='icon-edit bigger-120'></i>通过"+
						"</button>&nbsp;"+
						"<button class='btn1'>"+
							"<i class='icon-edit bigger-120'></i>不通过"+
						"</button>"+
					"</td>"+
					"</tr>";
				}else{
					$tr = "<tr class='listtr'>"+ 
					"<td class='id'>"+result.list[i].commodity_id+"</td>"+
					"<td class='name'>"+ result.list[i].commodity_name+ "</td>"+
					"<td><img height='50' width='50' src='"+result.list[i].commodity_img+"' /></td>"+
					"<td>"+ result.list[i].commodity_count+ "</td>"+
					"<td>"+ result.list[i].commodity_quantitysold+ "</td>"+
					"<td>￥"+ result.list[i].commodity_price+ "</td>"+
					"<td>￥"+ result.list[i].commodity_tradeprice+ "</td>"+
					"<td>"+ commodity_state+ "</td>"+
					"<td class='delete'>" + 
						"<button class='btn'>"+
							"<i class='icon-edit bigger-120'></i>"+editcommodity_state+
						"</button>"+
					"</td>"+
					"</tr>";
				}
				
			}else{
				$tr = "<tr class='listtr'>"+ 
				"<td class='id'>"+result.list[i].commodity_id+"</td>"+
				"<td>"+ result.list[i].commodity_name+ "</td>"+
				"<td><img height='50' width='50' src='"+result.list[i].commodity_img+"' /></td>"+
				"<td>"+ result.list[i].commodity_count+ "</td>"+
				"<td>"+ result.list[i].commodity_quantitysold+ "</td>"+
				"<td>￥"+ result.list[i].commodity_price+ "</td>"+
				"<td>￥"+ result.list[i].commodity_tradeprice+ "</td>"+
				"<td>"+ commodity_state+ "</td>"+
				"</tr>";
			}
			
			
			$("tbody").append($tr);
		}
	}
	

	//点击编辑按钮
	$(".btn").live("click",function() {
		var $id = $(this).parents("tr").find(".id").text();
		var $but = $(this).parents("tr").find(".delete").find(".btn").text();
		var state = 1;
		if($but=="设置不可用"){
			state=2;
		}
		if($but=="恢复可用"){
			state=1;
		}
		if($but=="通过审核"){
			state=1;
		}

		$.jq_Confirm({
			message : "您确定要操作吗?",
			btnOkClick : function() {
				var url = "modifyState?id="+$id+"&state="+state;
				$.ajax({
					"url" : url,
					"type" : "get",
					"dataType" : "text",
					"success" : function(result) {
						alert("设置成功！");
						window.location.href="/inventory";
					}
				})
				//window.location.href = "/editcommodity?commodity_id=" + $id;
			}
		});
	})
	
	$(".btn1").live("click",function(){
		var $id = $(this).parents("tr").find(".id").text();
		$.jq_Confirm({
			message : "您确定要操作吗?",
			btnOkClick : function() {
				var url = "modifyState?id="+$id+"&state=4";
				$.ajax({
					"url" : url,
					"type" : "get",
					"dataType" : "text",
					"success" : function(result) {
						alert("设置成功！");
						window.location.href="/inventory";
					}
				})
			}
		});
	})
	
	
	//点击添加商品
	$("#tj").click(function(){
		$.ajax({
			"url" : "/queryCommodityId",
			"type" : "get",
			"dataType" : "json",
			"success" : function(result) {
				for (var i = 0; i < result.length; i++) {
					var option="<option value='"+result[i].commoditytype_id+"'>"+result[i].commoditytype_name+"</option>";
					$("#commodity_type_id").append(option);
				}
			}
		})
	})
	
	//单击进入详情页
	$(".listtr").live("click",function(){
		var $id = $(this).find(".id").text();
		window.location.href="/product_detail_page?id="+$id;
	})
	
});
var btn_delete = function(id) {

}